{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "71883d47-302e-426b-9206-a23eb68219c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] Script started\n",
      "[INFO] Reading config file: config.json\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'config.json'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 87\u001b[0m\n\u001b[1;32m     84\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[INFO] Script finished\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     86\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m---> 87\u001b[0m     main()\n",
      "Cell \u001b[0;32mIn[7], line 77\u001b[0m, in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m     75\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmain\u001b[39m():\n\u001b[1;32m     76\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[INFO] Script started\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 77\u001b[0m     config \u001b[38;5;241m=\u001b[39m read_config(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mconfig.json\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m     78\u001b[0m     source_file \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msource.txt\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m     79\u001b[0m     repo_url \u001b[38;5;241m=\u001b[39m read_source_file(source_file)\n",
      "Cell \u001b[0;32mIn[7], line 9\u001b[0m, in \u001b[0;36mread_config\u001b[0;34m(filename)\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mread_config\u001b[39m(filename):\n\u001b[1;32m      8\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[INFO] Reading config file: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfilename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 9\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(filename, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[1;32m     10\u001b[0m         config \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mload(file)\n\u001b[1;32m     11\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m[INFO] Config: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py:310\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m    303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m    304\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    305\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    306\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    307\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    308\u001b[0m     )\n\u001b[0;32m--> 310\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'config.json'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import subprocess\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# 读取配置文件\n",
    "def read_config(filename):\n",
    "    print(f\"[INFO] Reading config file: {filename}\")\n",
    "    with open(filename, 'r') as file:\n",
    "        config = json.load(file)\n",
    "    print(f\"[INFO] Config: {config}\")\n",
    "    return config\n",
    "\n",
    "# 读取source.txt中的仓库地址\n",
    "def read_source_file(filename):\n",
    "    print(f\"[INFO] Reading source file: {filename}\")\n",
    "    with open(filename, 'r') as file:\n",
    "        repo_url = file.read().strip()\n",
    "    print(f\"[INFO] Repository URL from source file: {repo_url}\")\n",
    "    return repo_url\n",
    "\n",
    "# 克隆仓库\n",
    "def clone_repo(repo_url):\n",
    "    print(f\"[INFO] Cloning repository from: {repo_url}\")\n",
    "    repo_name = repo_url.split('/')[-1].replace('.git', '')\n",
    "    subprocess.run(['git', 'clone', repo_url], check=True)\n",
    "    print(f\"[INFO] Cloned repository name: {repo_name}\")\n",
    "    return repo_name\n",
    "\n",
    "# 在b.com上创建新的仓库\n",
    "def create_repo_on_b(repo_name, config):\n",
    "    print(f\"[INFO] Creating new repository on b.com with name: {repo_name}\")\n",
    "    url = 'https://atomgit.com/api/v3/projects?_input_charset=utf-8'\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'PRIVATE-TOKEN': config['private_token']\n",
    "    }\n",
    "    payload = {\n",
    "        \"name\": repo_name,\n",
    "        \"path\": repo_name,\n",
    "        \"visibility_level\": 0,\n",
    "        \"namespace_id\": config['namespace_id'],\n",
    "        \"init_standard_service\": False,\n",
    "        \"open_department_id_paths\": [],\n",
    "        \"target_org_id\": config['target_org_id']\n",
    "    }\n",
    "    print(f\"[INFO] Payload for creating repo: {payload}\")\n",
    "    response = requests.post(url, headers=headers, data=json.dumps(payload))\n",
    "    if response.status_code == 201:\n",
    "        repo_url = response.json()['http_url_to_repo']\n",
    "        print(f\"[INFO] Repository {repo_name} created successfully on b.com\")\n",
    "        print(f\"[INFO] New repository URL: {repo_url}\")\n",
    "        return repo_url\n",
    "    else:\n",
    "        print(f\"[ERROR] Failed to create repository on b.com: {response.status_code}\")\n",
    "        print(f\"[ERROR] Response: {response.text}\")\n",
    "        return None\n",
    "\n",
    "# 配置新的remote并推送代码\n",
    "def push_to_new_repo(repo_name, new_repo_url):\n",
    "    print(f\"[INFO] Configuring new remote and pushing to new repository: {new_repo_url}\")\n",
    "    os.chdir(repo_name)\n",
    "    print(f\"[INFO] Current directory: {os.getcwd()}\")\n",
    "    subprocess.run(['git', 'remote', 'remove', 'origin'], check=True)\n",
    "    print(f\"[INFO] Removed existing remote 'origin'\")\n",
    "    subprocess.run(['git', 'remote', 'add', 'origin', new_repo_url], check=True)\n",
    "    print(f\"[INFO] Added new remote 'origin' with URL: {new_repo_url}\")\n",
    "    subprocess.run(['git', 'branch', '-m', 'master'], check=True)\n",
    "    print(f\"[INFO] Renamed branch to 'master'\")\n",
    "    subprocess.run(['git', 'push', '-u', 'origin', 'master'], check=True)\n",
    "    print(f\"[INFO] Pushed code to new repository\")\n",
    "    os.chdir('..')\n",
    "    print(f\"[INFO] Changed directory back to: {os.getcwd()}\")\n",
    "\n",
    "def main():\n",
    "    print(\"[INFO] Script started\")\n",
    "    config = read_config('config.json')\n",
    "    source_file = 'source.txt'\n",
    "    repo_url = read_source_file(source_file)\n",
    "    repo_name = clone_repo(repo_url)\n",
    "    new_repo_url = create_repo_on_b(repo_name, config)\n",
    "    if new_repo_url:\n",
    "        push_to_new_repo(repo_name, new_repo_url)\n",
    "    print(\"[INFO] Script finished\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
